package com.dynamixsoftware.printershare.smb;

import com.dynamixsoftware.printershare.smb.util.DES;
import com.dynamixsoftware.printershare.smb.util.Encdec;
import com.dynamixsoftware.printershare.smb.util.HMACT64;
import com.dynamixsoftware.printershare.smb.util.MD4;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.Principal;
import java.util.Arrays;
import java.util.Random;

public final class NtlmPasswordAuthentication
  implements Serializable, Principal
{
  public static final NtlmPasswordAuthentication ANONYMOUS = new NtlmPasswordAuthentication("", "", "");
  static final String BLANK = "";
  public static final NtlmPasswordAuthentication DEFAULT = new NtlmPasswordAuthentication(null);
  private static String DEFAULT_DOMAIN;
  static String DEFAULT_PASSWORD;
  private static String DEFAULT_USERNAME;
  private static final int LM_COMPATIBILITY = 3;
  private static final Random RANDOM = new Random();
  private static final byte[] S8 = { 75, 71, 83, 33, 64, 35, 36, 37 };
  private byte[] ansiHash;
  private byte[] clientChallenge = null;
  String domain;
  boolean hashesExternal = false;
  String password;
  private byte[] unicodeHash;
  String username;
  
  public NtlmPasswordAuthentication(String paramString)
  {
    this.password = null;
    this.username = null;
    this.domain = null;
    if (paramString != null) {}
    try
    {
      String str = unescape(paramString);
      paramString = str;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      label42:
      int i;
      int j;
      int k;
      int m;
      break label42;
    }
    i = paramString.length();
    j = 0;
    k = 0;
    if (j < i)
    {
      m = paramString.charAt(j);
      if (m == 59)
      {
        this.domain = paramString.substring(0, j);
        k = j + 1;
      }
      while (m != 58)
      {
        j++;
        break;
      }
      this.password = paramString.substring(j + 1);
    }
    this.username = paramString.substring(k, j);
    initDefaults();
    if (this.domain == null) {
      this.domain = DEFAULT_DOMAIN;
    }
    if (this.username == null) {
      this.username = DEFAULT_USERNAME;
    }
    if (this.password == null) {
      this.password = DEFAULT_PASSWORD;
    }
  }
  
  public NtlmPasswordAuthentication(String paramString1, String paramString2, String paramString3)
  {
    int i = paramString2.indexOf('@');
    if (i > 0)
    {
      paramString1 = paramString2.substring(i + 1);
      paramString2 = paramString2.substring(0, i);
    }
    for (;;)
    {
      this.domain = paramString1;
      this.username = paramString2;
      this.password = paramString3;
      initDefaults();
      if (paramString1 == null) {
        this.domain = DEFAULT_DOMAIN;
      }
      if (paramString2 == null) {
        this.username = DEFAULT_USERNAME;
      }
      if (paramString3 == null) {
        this.password = DEFAULT_PASSWORD;
      }
      return;
      int j = paramString2.indexOf('\\');
      if (j > 0)
      {
        paramString1 = paramString2.substring(0, j);
        paramString2 = paramString2.substring(j + 1);
      }
    }
  }
  
  private static void E(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, byte[] paramArrayOfByte3)
  {
    byte[] arrayOfByte1 = new byte[7];
    byte[] arrayOfByte2 = new byte[8];
    for (int i = 0; i < paramArrayOfByte1.length / 7; i++)
    {
      System.arraycopy(paramArrayOfByte1, i * 7, arrayOfByte1, 0, 7);
      new DES(arrayOfByte1).encrypt(paramArrayOfByte2, arrayOfByte2);
      System.arraycopy(arrayOfByte2, 0, paramArrayOfByte3, i * 8, 8);
    }
  }
  
  private static byte[] computeResponse(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, byte[] paramArrayOfByte3, int paramInt1, int paramInt2)
  {
    HMACT64 localHMACT64 = new HMACT64(paramArrayOfByte1);
    localHMACT64.update(paramArrayOfByte2);
    localHMACT64.update(paramArrayOfByte3, paramInt1, paramInt2);
    byte[] arrayOfByte1 = localHMACT64.digest();
    byte[] arrayOfByte2 = new byte[arrayOfByte1.length + paramArrayOfByte3.length];
    System.arraycopy(arrayOfByte1, 0, arrayOfByte2, 0, arrayOfByte1.length);
    System.arraycopy(paramArrayOfByte3, 0, arrayOfByte2, arrayOfByte1.length, paramArrayOfByte3.length);
    return arrayOfByte2;
  }
  
  public static byte[] getLMv2Response(String paramString1, String paramString2, String paramString3, byte[] paramArrayOfByte1, byte[] paramArrayOfByte2)
  {
    try
    {
      byte[] arrayOfByte = new byte[24];
      MD4 localMD4 = new MD4();
      localMD4.update(paramString3.getBytes("UTF-16LE"));
      HMACT64 localHMACT641 = new HMACT64(localMD4.digest());
      localHMACT641.update(paramString2.toUpperCase().getBytes("UTF-16LE"));
      localHMACT641.update(paramString1.toUpperCase().getBytes("UTF-16LE"));
      HMACT64 localHMACT642 = new HMACT64(localHMACT641.digest());
      localHMACT642.update(paramArrayOfByte1);
      localHMACT642.update(paramArrayOfByte2);
      localHMACT642.digest(arrayOfByte, 0, 16);
      System.arraycopy(paramArrayOfByte2, 0, arrayOfByte, 16, 8);
      return arrayOfByte;
    }
    catch (Exception localException) {}
    return null;
  }
  
  public static byte[] getNTLM2Response(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, byte[] paramArrayOfByte3)
  {
    byte[] arrayOfByte1 = new byte[8];
    try
    {
      MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
      localMessageDigest.update(paramArrayOfByte2);
      localMessageDigest.update(paramArrayOfByte3, 0, 8);
      System.arraycopy(localMessageDigest.digest(), 0, arrayOfByte1, 0, 8);
      byte[] arrayOfByte2 = new byte[21];
      System.arraycopy(paramArrayOfByte1, 0, arrayOfByte2, 0, 16);
      byte[] arrayOfByte3 = new byte[24];
      E(arrayOfByte2, arrayOfByte1, arrayOfByte3);
      return arrayOfByte3;
    }
    catch (GeneralSecurityException localGeneralSecurityException)
    {
      throw new RuntimeException("MD5", localGeneralSecurityException);
    }
  }
  
  public static byte[] getNTLMResponse(String paramString, byte[] paramArrayOfByte)
  {
    byte[] arrayOfByte1 = new byte[21];
    byte[] arrayOfByte2 = new byte[24];
    try
    {
      byte[] arrayOfByte4 = paramString.getBytes("UTF-16LE");
      arrayOfByte3 = arrayOfByte4;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      for (;;)
      {
        MD4 localMD4;
        label48:
        byte[] arrayOfByte3 = null;
      }
    }
    localMD4 = new MD4();
    localMD4.update(arrayOfByte3);
    try
    {
      localMD4.digest(arrayOfByte1, 0, 16);
      E(arrayOfByte1, paramArrayOfByte, arrayOfByte2);
      return arrayOfByte2;
    }
    catch (Exception localException)
    {
      break label48;
    }
  }
  
  public static byte[] getNTLMv2Response(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, byte[] paramArrayOfByte3, long paramLong, byte[] paramArrayOfByte4)
  {
    if (paramArrayOfByte4 != null) {}
    for (int i = paramArrayOfByte4.length;; i = 0)
    {
      byte[] arrayOfByte = new byte[4 + (i + 28)];
      Encdec.enc_uint32le(257, arrayOfByte, 0);
      Encdec.enc_uint32le(0, arrayOfByte, 4);
      Encdec.enc_uint64le(paramLong, arrayOfByte, 8);
      System.arraycopy(paramArrayOfByte3, 0, arrayOfByte, 16, 8);
      Encdec.enc_uint32le(0, arrayOfByte, 24);
      if (paramArrayOfByte4 != null) {
        System.arraycopy(paramArrayOfByte4, 0, arrayOfByte, 28, i);
      }
      Encdec.enc_uint32le(0, arrayOfByte, i + 28);
      return computeResponse(paramArrayOfByte1, paramArrayOfByte2, arrayOfByte, 0, arrayOfByte.length);
    }
  }
  
  public static byte[] getPreNTLMResponse(String paramString, byte[] paramArrayOfByte)
  {
    byte[] arrayOfByte1 = new byte[14];
    byte[] arrayOfByte2 = new byte[21];
    byte[] arrayOfByte3 = new byte[24];
    try
    {
      byte[] arrayOfByte4 = paramString.toUpperCase().getBytes("ISO-8859-1");
      int i = arrayOfByte4.length;
      if (i > 14) {
        i = 14;
      }
      System.arraycopy(arrayOfByte4, 0, arrayOfByte1, 0, i);
      E(arrayOfByte1, S8, arrayOfByte2);
      E(arrayOfByte2, paramArrayOfByte, arrayOfByte3);
      return arrayOfByte3;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      throw new RuntimeException("Try setting jcifs.encoding=US-ASCII", localUnsupportedEncodingException);
    }
  }
  
  private void getUserSessionKey(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, int paramInt)
    throws SmbException
  {
    if (this.hashesExternal) {
      return;
    }
    MD4 localMD4;
    try
    {
      localMD4 = new MD4();
      localMD4.update(this.password.getBytes("UTF-16LE"));
      switch (3)
      {
      default: 
        localMD4.update(localMD4.digest());
        localMD4.digest(paramArrayOfByte2, paramInt, 16);
        return;
      }
    }
    catch (Exception localException)
    {
      throw new SmbException("", localException);
    }
    localMD4.update(localMD4.digest());
    localMD4.digest(paramArrayOfByte2, paramInt, 16);
    return;
    if (this.clientChallenge == null)
    {
      this.clientChallenge = new byte[8];
      RANDOM.nextBytes(this.clientChallenge);
    }
    HMACT64 localHMACT641 = new HMACT64(localMD4.digest());
    localHMACT641.update(this.username.toUpperCase().getBytes("UTF-16LE"));
    localHMACT641.update(this.domain.toUpperCase().getBytes("UTF-16LE"));
    byte[] arrayOfByte = localHMACT641.digest();
    HMACT64 localHMACT642 = new HMACT64(arrayOfByte);
    localHMACT642.update(paramArrayOfByte1);
    localHMACT642.update(this.clientChallenge);
    HMACT64 localHMACT643 = new HMACT64(arrayOfByte);
    localHMACT643.update(localHMACT642.digest());
    localHMACT643.digest(paramArrayOfByte2, paramInt, 16);
  }
  
  private static void initDefaults()
  {
    if (DEFAULT_DOMAIN != null) {
      return;
    }
    DEFAULT_DOMAIN = "?";
    DEFAULT_USERNAME = "GUEST";
    DEFAULT_PASSWORD = "";
  }
  
  public static byte[] nTOWFv1(String paramString)
  {
    if (paramString == null) {
      throw new RuntimeException("Password parameter is required");
    }
    try
    {
      MD4 localMD4 = new MD4();
      localMD4.update(paramString.getBytes("UTF-16LE"));
      byte[] arrayOfByte = localMD4.digest();
      return arrayOfByte;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      throw new RuntimeException(localUnsupportedEncodingException.getMessage());
    }
  }
  
  public static byte[] nTOWFv2(String paramString1, String paramString2, String paramString3)
  {
    try
    {
      MD4 localMD4 = new MD4();
      localMD4.update(paramString3.getBytes("UTF-16LE"));
      HMACT64 localHMACT64 = new HMACT64(localMD4.digest());
      localHMACT64.update(paramString2.toUpperCase().getBytes("UTF-16LE"));
      localHMACT64.update(paramString1.getBytes("UTF-16LE"));
      byte[] arrayOfByte = localHMACT64.digest();
      return arrayOfByte;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      throw new RuntimeException(localUnsupportedEncodingException.getMessage());
    }
  }
  
  private static String unescape(String paramString)
    throws NumberFormatException, UnsupportedEncodingException
  {
    byte[] arrayOfByte = new byte[1];
    if (paramString == null) {
      return null;
    }
    int i = paramString.length();
    char[] arrayOfChar = new char[i];
    int j = 0;
    int k = 0;
    int m = 0;
    if (k < i)
    {
      int n;
      switch (j)
      {
      default: 
        n = m;
      }
      for (;;)
      {
        k++;
        m = n;
        break;
        int i1 = paramString.charAt(k);
        if (i1 == 37)
        {
          j = 1;
          n = m;
        }
        else
        {
          n = m + 1;
          arrayOfChar[m] = i1;
          continue;
          arrayOfByte[0] = ((byte)(0xFF & Integer.parseInt(paramString.substring(k, k + 2), 16)));
          n = m + 1;
          arrayOfChar[m] = new String(arrayOfByte, 0, 1, "ASCII").charAt(0);
          k++;
          j = 0;
        }
      }
    }
    return new String(arrayOfChar, 0, m);
  }
  
  public boolean equals(Object paramObject)
  {
    if ((paramObject instanceof NtlmPasswordAuthentication))
    {
      NtlmPasswordAuthentication localNtlmPasswordAuthentication = (NtlmPasswordAuthentication)paramObject;
      if ((localNtlmPasswordAuthentication.domain.toUpperCase().equals(this.domain.toUpperCase())) && (localNtlmPasswordAuthentication.username.toUpperCase().equals(this.username.toUpperCase())))
      {
        if ((this.hashesExternal) && (localNtlmPasswordAuthentication.hashesExternal)) {
          if ((!Arrays.equals(this.ansiHash, localNtlmPasswordAuthentication.ansiHash)) || (!Arrays.equals(this.unicodeHash, localNtlmPasswordAuthentication.unicodeHash))) {}
        }
        while ((!this.hashesExternal) && (this.password.equals(localNtlmPasswordAuthentication.password)))
        {
          return true;
          return false;
        }
      }
    }
    return false;
  }
  
  byte[] getAnsiHash(byte[] paramArrayOfByte)
  {
    if (this.hashesExternal) {
      return this.ansiHash;
    }
    switch (3)
    {
    default: 
      return getPreNTLMResponse(this.password, paramArrayOfByte);
    case 0: 
    case 1: 
      return getPreNTLMResponse(this.password, paramArrayOfByte);
    case 2: 
      return getNTLMResponse(this.password, paramArrayOfByte);
    }
    if (this.clientChallenge == null)
    {
      this.clientChallenge = new byte[8];
      RANDOM.nextBytes(this.clientChallenge);
    }
    return getLMv2Response(this.domain, this.username, this.password, paramArrayOfByte, this.clientChallenge);
  }
  
  public String getDomain()
  {
    return this.domain;
  }
  
  public String getName()
  {
    if ((this.domain.length() > 0) && (!this.domain.equals("?"))) {}
    for (int i = 1; i != 0; i = 0) {
      return this.domain + "\\" + this.username;
    }
    return this.username;
  }
  
  public String getPassword()
  {
    return this.password;
  }
  
  byte[] getSigningKey(byte[] paramArrayOfByte)
    throws SmbException
  {
    switch (3)
    {
    default: 
      return null;
    case 0: 
    case 1: 
    case 2: 
      byte[] arrayOfByte = new byte[40];
      getUserSessionKey(paramArrayOfByte, arrayOfByte, 0);
      System.arraycopy(getUnicodeHash(paramArrayOfByte), 0, arrayOfByte, 16, 24);
      return arrayOfByte;
    }
    throw new SmbException("NTLMv2 requires extended security (jcifs.smb.client.useExtendedSecurity must be true if jcifs.smb.lmCompatibility >= 3)");
  }
  
  byte[] getUnicodeHash(byte[] paramArrayOfByte)
  {
    if (this.hashesExternal) {
      return this.unicodeHash;
    }
    switch (3)
    {
    default: 
      return getNTLMResponse(this.password, paramArrayOfByte);
    case 0: 
    case 1: 
    case 2: 
      return getNTLMResponse(this.password, paramArrayOfByte);
    }
    return new byte[0];
  }
  
  public String getUsername()
  {
    return this.username;
  }
  
  public int hashCode()
  {
    return getName().toUpperCase().hashCode();
  }
  
  public String toString()
  {
    return getName();
  }
}


/* Location:           C:\Users\Admin\Desktop\Decompiler\JAD\jd-gui-0.3.6.windows\classes_dex2jar.jar
 * Qualified Name:     com.dynamixsoftware.printershare.smb.NtlmPasswordAuthentication
 * JD-Core Version:    0.7.0.1
 */